//
//  ModifyPhotoViewController.m
//  HpnotiqCustomize
//
//  Created by Harmandeep Singh on 09/06/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ModifyPhotoViewController.h"
#import "ModifyItemView.h"
#import "ToolbarShapeItemView.h"


@interface ModifyPhotoViewController()

- (void)loadBottomToolbar;
- (void)loadPhotoView;
- (void)showBottomToolbarAnimated:(BOOL)animated;

@end



@implementation ModifyPhotoViewController

@synthesize mOriginalImage;
@synthesize mImageIsShotFromApp;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    self.title = @"Enrich Photo";
    
    self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:nil] autorelease];
    
    
    // First we load bottomToolBar but we dont add it on to the view of this controller.
    [self loadBottomToolbar];
    
    // loadPhotoView should always be called after loadBottomToolbar
    [self loadPhotoView];
    
    // Now add the photoView on to the view of this controller
    [self.view addSubview:mPhotoView];
    
    // Now after adding the photoview we shall add the bottom toolbar on to the view of this controller
    [self.view addSubview:mBottomToolbar];
    

    
    
    
    
    for(int i=0; i<3; i++)
    {
        UIImage *heartImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heart" ofType:@"png"]];
        UIImageView *heartImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,
                                                                                    0,
                                                                                    heartImage.size.width,
                                                                                    heartImage.size.height)];
        heartImageView.image = heartImage;
        [heartImage release];
        
        ModifyItemView *heartItemView = [[ModifyItemView alloc] initWithView:heartImageView andFrame:heartImageView.frame];
        [heartImageView release];
        
        
        
        [mPhotoView addModifyItemView:heartItemView withOrigin:CGPointMake(heartItemView.frame.origin.x,
                                                                           heartItemView.frame.origin.y)];
        [heartItemView release];
    }
    
    
}

- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    [self showBottomToolbarAnimated:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
    
    [mPhotoView release];
    [mBottomToolbar release];
    [mOriginalImage release];
    [super dealloc];
}




- (void)loadBottomToolbar
{
    [mBottomToolbar removeFromSuperview];
    [mBottomToolbar release];
    mBottomToolbar = [[ModifyBottomToolbar alloc] initWithFrame:CGRectZero andShapeFilesNames:nil];
    
    mBottomToolbar.frame = CGRectMake(0,
                                      self.view.frame.size.height,
                                      mBottomToolbar.frame.size.width,
                                      mBottomToolbar.frame.size.height);
    mBottomToolbar.viewDelegate = self;
}

- (void)loadPhotoView
{
    [mPhotoView removeFromSuperview];
    [mPhotoView release];
    mPhotoView = [[ModifyPhotoView alloc] initWithImage:self.mOriginalImage withinRect:CGRectMake(0,
                                                                                                  0,
                                                                                                  self.view.frame.size.width,
                                                                                                  self.view.frame.size.height - mBottomToolbar.frame.size.height)];
    mPhotoView.viewDelegate = self;
}


- (void)showBottomToolbarAnimated:(BOOL)animated
{
    CGFloat desiredYCoordinate = self.view.frame.size.height - mBottomToolbar.frame.size.height;
    
    if(mBottomToolbar.frame.origin.y == desiredYCoordinate)
        return;
    
    if(animated)
    {
        [UIView animateWithDuration:0.3
                              delay:0.0
                            options:UIViewAnimationCurveEaseOut
                         animations:^{
                             
                             mBottomToolbar.frame = CGRectMake(mBottomToolbar.frame.origin.x,
                                                               desiredYCoordinate,
                                                               mBottomToolbar.frame.size.width,
                                                               mBottomToolbar.frame.size.height);
                         } 
                         completion:^(BOOL finished) {
                             
                         }];
        
        
    }
    else
    {
        mBottomToolbar.frame = CGRectMake(mBottomToolbar.frame.origin.x,
                                          desiredYCoordinate,
                                          mBottomToolbar.frame.size.width,
                                          mBottomToolbar.frame.size.height);
    }
}












- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	
    
    
    UITouch *touch = [touches anyObject];
    UIView *touchedView = [touch view];
    
    NSLog(@"Touches Began %@", touchedView);
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	
	UITouch *touch = [touches anyObject];
    UIView *touchedView = [touch view];
    
    
    if([touchedView isKindOfClass:[ToolbarShapeItemView class]])
    {
        CGPoint touchPoint = [touch locationInView:mBottomToolbar];
        
        if(![(ToolbarShapeItemView *)touchedView mIsBeingMoved])
        {
     
            [(ToolbarShapeItemView *)touchedView setMIsBeingMoved:YES];
            [UIView animateWithDuration:0.3
                                  delay:0.0
                                options:UIViewAnimationCurveLinear
                             animations:^{
                                 
                                 touchedView.center = touchPoint;
                             } 
                             completion:^(BOOL finished) {
                                 
                                 
                             }];
        }
        
        touchedView.center = touchPoint;
        
    }
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	
	UITouch *touch = [touches anyObject];
    UIView *touchedView = [touch view];
    
    if([touchedView isKindOfClass:[ToolbarShapeItemView class]])
    {
        [(ToolbarShapeItemView *)touchedView setMIsBeingMoved:NO];
            
        CGPoint locationInSelfView = [touch locationInView:self.view];
        if(CGRectContainsPoint(mPhotoView.frame, locationInSelfView))
        {
            SVGView *clonedView = [(ToolbarShapeItemView *)touchedView clone];

    
            ModifyItemView *newItemView = [[ModifyItemView alloc] initWithView:clonedView andFrame:CGRectMake(0,
                                                                                                              0,
                                                                                                              clonedView.frame.size.width,
                                                                                                              clonedView.frame.size.height)];
            
            
            CGPoint originForNewView = [mBottomToolbar convertPoint:CGPointMake(touchedView.frame.origin.x,
                                                                                touchedView.frame.origin.y)
                                                             toView:mPhotoView];
                                                
            [mPhotoView addModifyItemView:newItemView withOrigin:originForNewView];
            [newItemView release];
            
            

            
            touchedView.frame = [(ToolbarShapeItemView *)touchedView mOriginalFrameInToolbar];
            touchedView.alpha = 0.0;
            
            [UIView animateWithDuration:0.3
                             animations:^{
                                
                                 touchedView.alpha = 1.0;
                             }];
        }
        else
        {
            [UIView animateWithDuration:0.3
                             animations:^{
                                
                                 touchedView.frame = [(ToolbarShapeItemView *)touchedView mOriginalFrameInToolbar];
                             }];
        }
    }
}


- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
	
    //	/*
    //     To impose as little impact on the device as possible, simply set the placard view's center and transformation to the original values.
    //     */
    //	placardView.center = self.center;
    //	placardView.transform = CGAffineTransformIdentity;
    
    
    NSLog(@"Touches Cacnelled");
}





#pragma mark - ModifyPhotoView Delegate Methods

- (void)modifyPhotoView:(ModifyPhotoView *)photoView newlySelectedObject:(ModifyItemView *)itemView
{
    [mBottomToolbar modifyTheToolbarForSelectionOfModifyItemView:itemView];
}



#pragma mark - ModifyBottomToolbar Delegate Methods

- (void)modifyBottomToolbar:(ModifyBottomToolbar *)toolbar userChangedValueOnSizeSliderToValue:(CGFloat)newValue
{
    [mPhotoView.mSelectedItemView makeScaleTransformToValue:newValue];
}

- (void)modifyBottomToolbar:(ModifyBottomToolbar *)toolbar userChangedValueOnRotationSliderToValue:(CGFloat)newValue
{
    [mPhotoView.mSelectedItemView makeScaleRotationToValue:newValue];
}


@end
